import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/app/models/subcategory.dart';
import 'package:flutter_app/app/networking/subcategory_api_service.dart';
import 'package:flutter_app/resources/widgets/movie_view_widget.dart';
import 'package:nylo_framework/nylo_framework.dart';

class CategoryTabView extends StatefulWidget {
  final List<String> secondTab;
  final String type;
  const CategoryTabView({
    super.key,
    this.type = "movie",
    required this.secondTab,
  });

  @override
  createState() => _CategoryTabViewState();
}

class _CategoryTabViewState extends NyState<CategoryTabView> {
  late int _selectedIndex;
  late int page;
  late Subcategory subcategory;
  SubcategoryApiService subcategoryApiService = SubcategoryApiService();

  @override
  get init => () {
        _selectedIndex = 0;
        page = 0;
      };

  @override
  Widget view(BuildContext context) {
    return Column(
      children: [
        Container(
          color: Colors.white,
          padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
          height: 40,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: widget.secondTab.length,
            itemBuilder: (context, index) {
              return GestureDetector(
                onTap: () async {
                  _selectedIndex = index;
                  setState(() {});
                },
                child: Container(
                  alignment: Alignment.center, // 使每个标签垂直居中
                  padding: const EdgeInsets.symmetric(horizontal: 8.0),
                  child: AnimatedDefaultTextStyle(
                    textAlign: TextAlign.center,
                    duration: const Duration(milliseconds: 200),
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: _selectedIndex == index
                          ? FontWeight.bold
                          : FontWeight.normal,
                      color: Colors.black87,
                    ),
                    child: Text(widget.secondTab[index]),
                  ),
                ),
              );
            },
          ),
        ),
        Expanded(
          child: NyPullToRefresh.grid(
            key: ValueKey(_selectedIndex),
            child: (BuildContext context, dynamic data) {
              data as SubcategorySubjects;
              return MovieView(
                item: data,
              );
            },
            data: (int iteration) async {
              subcategory = await subcategoryApiService.fetchData(
                  widget.type, widget.secondTab[_selectedIndex], iteration - 1);
              return subcategory.subjects;
            },
            crossAxisCount: 2,
            footerLoadingIcon: CupertinoActivityIndicator(),
            loadingStyle: LoadingStyle.normal(
              child: CupertinoActivityIndicator(),
            ),
          ),
        ),
      ],
    );
  }
}
